from tkinter import *
import tkinter.messagebox
from tkinter import ttk
from tkinter import font
from src.backend.login_b import LoginBackend
from src.frontend.menu_f import MenuFrontend

def main():
    root = Tk()
    app = MainWindow(root)
    # start the Tk event loop so the window stays open
    root.mainloop()

#MAIN WINDOW FOR LOG IN
class MainWindow:
    # constructor
    def __init__(self, master):
        # public data mambers
        self.master = master
        self.backend = LoginBackend()
        self.master.title("HOSPITAL MANAGEMENT SYSTEM")
        self.master.geometry("800x500+0+0")
        self.master.config(bg="#C0C0C0")
        self.frame = Frame(self.master, bg="#C0C0C0")
        self.frame.pack()

        self.Username = StringVar()
        self.Password = StringVar()

        self.lblTitle = Label(self.frame, text="HOSPITAL MANAGEMENT SYSTEM", font="Arial 16 bold", bg="#C0C0C0", fg="#000080")
        self.lblTitle.grid(row=0, column=0, columnspan=2, pady=40)
        #======================
        self.LoginFrame = Frame(self.frame, width=400, height=80, relief="groove", bg="#C0C0C0", bd=3)
        self.LoginFrame.grid(row=1, column=0, padx=20, pady=20)
        #======================
        self.lblUsername = Label(self.LoginFrame, text="USERNAME", font="Arial 12 bold", bg="#C0C0C0", bd=10)
        self.lblUsername.grid(row=0, column=0, sticky=W, padx=10, pady=10)
        self.lblUsername = Entry(self.LoginFrame, font="Arial 12", bd=2, relief="sunken", textvariable=self.Username)
        self.lblUsername.grid(row=0, column=1, padx=10, pady=10)

        self.lblPassword = Label(self.LoginFrame, text="PASSWORD", font="Arial 12 bold", bg="#C0C0C0", bd=10)
        self.lblPassword.grid(row=1, column=0, sticky=W, padx=10, pady=10)
        self.lblPassword = Entry(self.LoginFrame, font="Arial 12", bd=2, relief="sunken", textvariable=self.Password, show="*")
        self.lblPassword.grid(row=1, column=1, padx=10, pady=10)

        self.button1 = Button(self.LoginFrame, text="LOGIN", width=12, font="Arial 11 bold", bg="#C0C0C0", relief="raised", bd=2, command=self.login)
        self.button1.grid(row=2, column=1, padx=10, pady=15)

        self.button2 = Button(self.LoginFrame, text="EXIT", width=12, font="Arial 11 bold", bg="#C0C0C0", relief="raised", bd=2, command=self.Exit)
        self.button2.grid(row=2, column=0, padx=10, pady=15)

    def login(self):
        """Handle login button click"""
        try:
            username = self.Username.get()
            password = self.Password.get()
            
            is_valid, result = self.backend.validate_login(username, password)
            
            if is_valid:
                user_type = result
                # Directly enter system without confirmation message
                self.master.destroy()
                
                # Open menu window with user type
                menu_root = Tk()
                menu_app = MenuFrontend(menu_root, user_type)
                menu_root.mainloop()
            else:
                tkinter.messagebox.showerror("HOSPITAL DATABASE SYSTEM", result)
                
        except Exception as e:
            tkinter.messagebox.showerror("HOSPITAL DATABASE SYSTEM", f"Login error: {str(e)}")

    def Exit(self):
        """Exit the application"""
        self.master.destroy()

if __name__ == "__main__":
    main()

